var express = require("express");
var path = require("path");
var cookieParser = require("cookie-parser");
var logger = require("morgan");

var indexRouter = require("./routes/index");
var usersRouter = require("./routes/users");

var app = express();
const ws = require("ws");
const clients = []; // 存储所有的客户端连接
const wss = new ws.Server({ port: 8080 });
// 监听连接事件
wss.on("connection", (ws) => {
  clients.push({
    ws,
  });
  ws.on("message", (message) => {
    clients.forEach((item) => {
      item.ws.send(String(message + "666"));
    });
  });
});

app.use(logger("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, "public")));

app.use("/", indexRouter);
app.use("/users", usersRouter);

module.exports = app;
